﻿#建议保存编码为：bom头 + utf8

param
(
	[ValidateNotNullOrEmpty()]
	[Alias("config_file")][String]$电壳别名宝配置文档全路径 = '~/.power_alias11.xml'
)

if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
	Write-Warning '信息：不建议在win中使用'
	Pause
}

#导入模块，加载dll
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
	Add-Type -Path "${PSScriptRoot}/Terminal.Gui.ps51.dll" -ErrorAction Stop
}
else
{
	Add-Type -Path "${PSScriptRoot}/Terminal.Gui.dll" -ErrorAction Stop
}

#region code

if (Test-Path -LiteralPath $电壳别名宝配置文档全路径)
{
	$电壳别名宝配置 = Import-Clixml -LiteralPath $电壳别名宝配置文档全路径
}
else
{
	$电壳别名宝配置 = @()
	$别名s = 'get-date','Get-ChildItem ~','从根目录，查找所有.ps1文件','b','c','d','e','f','g','电壳别名宝v0.13','Power Alias v0.13','config:','~/.power_alias11.xml','~/.power_alias12.xml','主治病症：记不住linux命令','n','o','p','q','r','s','t','u','v','w','x','y','z'
	$命令s = 'get-date','Get-ChildItem ~','find / -iname "*.ps1"','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
	for ($outi = 0;$outi -lt $别名s.Length ;$outi++)
	{
		$hash = @{ k1 = $别名s[$outi] ; v1 = $命令s[$outi] }
		$电壳别名宝配置 = $电壳别名宝配置 + $hash
	}
	$电壳别名宝配置 | Export-Clixml  -LiteralPath $电壳别名宝配置文档全路径
}

#endregion code

#region tui
[Terminal.Gui.Application]::Init()
$Window = [Terminal.Gui.Window]::new()
$Window.Title = "支持中文，只有1层窗口的（用多个脚本实现多个窗口的），电壳别名宝 v0.13。用回车执行。按 Ctrl + Q 退出"

$Frame1 = [Terminal.Gui.FrameView]::new()
$Frame1.Width = [Terminal.Gui.Dim]::Percent(40)
$Frame1.Height = [Terminal.Gui.Dim]::Fill()
$Frame1.Title = "别名栏"

$ListView = [Terminal.Gui.ListView]::new()
$ListView.Width = [Terminal.Gui.Dim]::Fill()
$ListView.Height = [Terminal.Gui.Dim]::Fill()
$ListView.SetSource($($电壳别名宝配置).k1)

$ListView.add_SelectedItemChanged( {
		$别名文本框.text = $电壳别名宝配置[$ListView.SelectedItem].k1
		$命令文本框.text = $电壳别名宝配置[$ListView.SelectedItem].v1
	})

$ListView.add_KeyPress( {
		param($arg)
		if ($arg.KeyEvent.Key.ToString() -eq "enter" -and $ListView.hasfocus)
		{
			$Global:_power_alias_cmd1 = $命令文本框.text.tostring()
			[Terminal.Gui.Application]::Shutdown()
		}
	})

$Frame1.Add($ListView)

#-------------------------------------------
$Frame2 = [Terminal.Gui.FrameView]::new()
$Frame2.Width = [Terminal.Gui.Dim]::Percent(60)
$Frame2.Height = [Terminal.Gui.Dim]::Fill()
$Frame2.X = [Terminal.Gui.Pos]::Right($Frame1)
$Frame2.Title = "详细信息栏"

$别名标签 = [Terminal.Gui.Label]::new()
$别名标签.text = '别 名：'
$别名标签.y = 3
$别名标签.Width = [Terminal.Gui.Dim]::Fill()
$Frame2.Add($别名标签)

$别名文本框 = [Terminal.Gui.Textfield]::new()
$别名文本框.y = 4
$别名文本框.Width = [Terminal.Gui.Dim]::Fill()
$Frame2.Add($别名文本框)

$命令标签 = [Terminal.Gui.Label]::new()
$命令标签.text = 'PowerShell命 令：'
$命令标签.y = 7
$命令标签.Width = [Terminal.Gui.Dim]::Fill()
$Frame2.Add($命令标签)

$命令文本框 = [Terminal.Gui.Textfield]::new()
$命令文本框.y = 8
$命令文本框.text = ''
$命令文本框.Width = [Terminal.Gui.Dim]::Fill()
$命令文本框.Height = 3
$Frame2.Add($命令文本框)

$保存按钮 = [Terminal.Gui.Button]::new()
$保存按钮.y = 10
$保存按钮.Text = '保 存 命 令 和 别 名'
$保存按钮.add_Clicked( {
		$电壳别名宝配置[$ListView.SelectedItem].k1 = $别名文本框.text.tostring()
		$电壳别名宝配置[$ListView.SelectedItem].v1 = $命令文本框.text.tostring()
		$电壳别名宝配置 | Export-Clixml  -LiteralPath $电壳别名宝配置文档全路径
		$电壳别名宝配置 = Import-Clixml -LiteralPath $电壳别名宝配置文档全路径
		$ListView.SetSource($($电壳别名宝配置).k1)
	})
$Frame2.Add($保存按钮)

#-------------------------------------------
$Window.Add($Frame1)
$Window.Add($Frame2)
#endregion tui
[Terminal.Gui.Application]::Top.Add($Window)
[void][Terminal.Gui.Application]::Run()
[void][Terminal.Gui.Application]::Shutdown()

Invoke-Expression -Command $Global:_power_alias_cmd1
